package com.kunminx.viabus.repertory.bean;

import com.kunminx.viabus.business.constant.Configs;
import com.kunminx.viabus.repertory.model.AppDatabase;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class NoteBeanTest {

    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");

    private final Ability ability = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentTopAbility();
    private final AbilitySlice abilitySlice = AbilityDelegatorRegistry.getAbilityDelegator().getCurrentAbilitySlice(ability);

    private OrmContext ormContext;
    private DatabaseHelper databaseHelper;

    private String title;

    @Before
    public void setUp() throws Exception {
        databaseHelper = new DatabaseHelper(abilitySlice);
        ormContext = databaseHelper.getOrmContext("AppDatabase", Configs.DB_PATH, AppDatabase.class);


    }

    @Test
    public void BeanTest() {
        title = "test-title";
        Long id = 100L;
        String date = "20201-06-21";
        String url = "https://upload-images.jianshu.io/upload_images/57036-fb9653da874d2447.jpg";

        NoteBean bean = new NoteBean();

        bean.setTitle(title);
        bean.setId(id);
        bean.setDate(date);
        bean.setImgUrl(url);


        boolean isSuccessed = ormContext.insert(bean);
        assertTrue(isSuccessed);
        isSuccessed = ormContext.flush();
        assertTrue(isSuccessed);

        OrmPredicates query = ormContext.where(NoteBean.class);
        List<NoteBean> newLists = ormContext.query(query);
        for (NoteBean list : newLists) {
            HiLog.error(LABEL, list.getTitle());
            HiLog.error(LABEL, list.getId() + "");
            HiLog.error(LABEL, list.getDate());
            HiLog.error(LABEL, list.getImgUrl());

            assertEquals(title, list.getTitle());
            assertEquals(id, list.getId());
            assertEquals(date, list.getDate());
            assertEquals(url, list.getImgUrl());
        }

        // update
        query.equalTo(title, title);
        NoteBean secondTitle = newLists.get(0);

        assertEquals(title, secondTitle.getTitle());

        title = "update-title";
        secondTitle.setTitle(title);
        ormContext.update(secondTitle);
        ormContext.flush();

        HiLog.error(LABEL, newLists.get(0).getTitle());
        assertEquals(title, newLists.get(0).getTitle());

        // del
        query.equalTo(title, title);
        NoteBean delTitle = newLists.get(0);

        assertEquals(title, delTitle.getTitle());

        ormContext.delete(delTitle);
        ormContext.flush();

        query = ormContext.where(NoteBean.class);
        newLists = ormContext.query(query);
        HiLog.error(LABEL, newLists.size() + "");
        assertEquals(0, newLists.size());
    }
}